package com.SouthernWall_404.AgeSuitcase.Common.API.GuiAPI.Animation;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;

public abstract class AbstractAnimation {


    public AbstractWidget abstractWidget;
    public AbstractAnimation next=null;
    public int x;
    public int y;
    public int time;
    public int now=0;
    public float rate=0;

    public AbstractAnimation(AbstractWidget abstractWidget,int time) {
        this.abstractWidget =abstractWidget;
        this.x=abstractWidget.getX();
        this.y=abstractWidget.getY();
        this.time=time;
    }

    public AbstractAnimation(AbstractWidget abstractWidget,int time,AbstractAnimation next) {
        this.abstractWidget =abstractWidget;
        this.x=abstractWidget.getX();
        this.y=abstractWidget.getY();
        this.time=time;
        this.next=next;

    }

    public void tick(GuiGraphics guiGraphics, float partialTicks)
    {
        rate=(float)now/time;

        if(rate>=1)
        {
            this.onFinished(guiGraphics,partialTicks);
        }
        this.render(guiGraphics,partialTicks);
        if(now<time)
        {
            now++;
        }
    }

    public void render(GuiGraphics guiGraphics,float partialTicks)
    {
        if (isFinished())
        {
            ensureFinalState(guiGraphics, partialTicks);
            return;
        }
    }
    public void onFinished(GuiGraphics guiGraphics,float partialTicks)
    {
        guiGraphics.setColor(1,1,1,1);
        if(this.next!=null)//有下个动画序列
        {
            this.next.tick(guiGraphics,partialTicks);//播放下一个
        }
        else
        {
            customOnFinished(guiGraphics,partialTicks);
        }
    }
    abstract void customOnFinished(GuiGraphics guiGraphics,float partialTicks);
    public boolean isFinished()
    {
        return this.rate>=1;
    }
    public void ensureFinalState(GuiGraphics guiGraphics, float partialTicks) {
        // 确保组件以不透明状态渲染
        guiGraphics.pose().pushPose();
        guiGraphics.setColor(1.0f, 1.0f, 1.0f, 1.0f);
        abstractWidget.render(guiGraphics, x, y, partialTicks);
        guiGraphics.pose().popPose();
    }

}
